from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *

from app.window.msg_box import Msg_box
from app.window.create_script_group_dialog import Create_script_group_dialog
from database.db import Database
from ui.Ui_script_editor_dialog import Ui_Dialog_script_editor


class Public_script_editor_signal(QObject):
    save = Signal(int)


class Public_script_editor_dialog(QDialog):
    
    
    def __init__(self, parent=None):
        super(Public_script_editor_dialog, self).__init__(parent)
        self.ui = Ui_Dialog_script_editor()
        self.ui.setupUi(self)
        self.setWindowTitle('公有脚本编辑器')
        self.setModal(True)  # 设置窗体为模态窗口，必须为True
        self.setWindowFlags(Qt.WindowCloseButtonHint)  # 设置窗体仅显示关闭按钮

        self.signal = Public_script_editor_signal()
        
        self.script_id:None

        self.ui.comboBox_group.activated.connect(self.slot_comboBox_active)
        
        self.create_group_name_dialog = Create_script_group_dialog()
        self.create_group_name_dialog.signal.save.connect(self.slot_update_group)

        self.ui.pushButton_save.clicked.connect(self.slot_save)
    
    def reload_group(self):
        # 重载脚本分组
        self.ui.comboBox_group.clear()
        db = Database()
        group_list = db.select('script_group',['name'])
        for data in group_list:
            self.ui.comboBox_group.addItem(data[0])
        self.ui.comboBox_group.addItem('<创建分组>')
        db.close_conn()

    def slot_comboBox_active(self,index:int):
        if self.ui.comboBox_group.currentText() == '<创建分组>':
            self.create_group_name_dialog.slot_show()


    def slot_update_group(self,name:str):
        self.reload_group()
        self.ui.comboBox_group.setCurrentText(name)
    
    def slot_save(self):
        group_name = self.ui.comboBox_group.currentText()
        if group_name == '<创建分组>':
            QMessageBox().information(self,'错误!','请选择正确的分组',QMessageBox.Ok)
            return
        
        data = {
            'name':self.ui.lineEdit_name.text(),
            'content':self.ui.textEdit_script.toPlainText(),
            'textfsm':self.ui.textEdit_textfsm.toPlainText(),
            'info':self.ui.textEdit_info.toPlainText(),
            'type':'public',
            'group_name':group_name,

        }
        # 更新数据库
        db = Database()
        fields = ['name','content','textfsm','info','type','group_name']
        values = (data['name'],data['content'],data['textfsm'],data['info'],
                  data['type'],data['group_name'],)
        db.update('scripts',fields,values,f'id={self.script_id}')
        
        db.close_conn()

        # 公有脚本管理器更新ui数据
        self.signal.save.emit(self.script_id)
        self.hide()
    
    def slot_show(self,script_id:int):
        self.script_id = script_id
        db = Database()
        fields = ['name','content','textfsm','info','group_name']
        script_list = db.select('scripts',fields,f'id={script_id}')
        db.close_conn()
        data = {
            'name':script_list[0][0],
            'content':script_list[0][1],
            'textfsm':script_list[0][2],
            'info':script_list[0][3],
            'group_name':script_list[0][4],
        }
        

        self.ui.lineEdit_name.setText(data['name'])
        self.ui.textEdit_script.setText(data['content'])
        self.ui.textEdit_textfsm.setText(data['textfsm'])
        self.ui.textEdit_info.setText(data['info'])
        
        # 重载脚本分组
        self.reload_group()
        
        self.ui.comboBox_group.setCurrentText(data['group_name'])

        self.show()